//
//  XJCRankViewController.m
//  On The Load
//
//  Created by 许嘉成 on 15/11/6.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "XJCRankViewController.h"
#import "AppTools.h"
#import "XJCVideoModel.h"
#import "XJCTimeTableViewCell.h"
#import "EntertainmentDetailsViewController.h"
#import <MJRefresh.h>

@interface XJCRankViewController ()<UITableViewDelegate,UITableViewDataSource,removeMyController>

@property (nonatomic, strong) UITableView *videoTV;
@property (nonatomic, strong) NSMutableArray *videoArray;
@property (nonatomic, weak) UIViewController *popController;

@end

@implementation XJCRankViewController

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.videoArray = [NSMutableArray array];
    [self createView];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getAFN)];
    self.videoTV.footer = footer;
    [self getAFN];
}
- (void)getAFN{
    
    
    
    NSString *urlStr = @"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=旅行&strategy=shareCount&vc=125&t=MjAxNTExMDUxNzE5MTYzMTcsNjk0NA%3D%3D&u=7e508a0fecf9bcb579f260fc0ed8fa4ef415a20e&net=wifi&v=1.8.1&f=iphone%20HTTP/1.1Host:%20baobab.wandoujia.com";
    NSString *strEncode = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [AppTools getAFN:strEncode andpara:nil andBlock:^(id result) {
        NSArray *bigArray = [result objectForKey:@"videoList"];
        for (NSMutableDictionary *dic in bigArray) {
            XJCVideoModel *model = [[XJCVideoModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            model.descrip = [dic objectForKey:@"description"];
            [self.videoArray addObject:model];
        }
        [self.videoTV reloadData];
        
    } andfailBlock:^(id result) {
        
        
    }];
}
#pragma  mark-- XJCTimeDetailViewController DataSource
-(void)popMyController:(NSInteger)index andDataArray:(NSMutableArray *)array
{
    
    EntertainmentDetailsViewController *detailVC = [[EntertainmentDetailsViewController
                                                     alloc]init];
    [UIView animateWithDuration:0.1 animations:^{
        detailVC.titleLabel.alpha = 0;
        detailVC.timeLabel.alpha = 0;
        detailVC.lineView.alpha = 0;
        detailVC.descLabel.alpha = 0;
        
    }];
    
    if (index != 0) {
        CGPoint point = self.videoTV.contentOffset;
        point.y += index * 200 * SCALEH;
        self.videoTV.contentOffset = point;
    }
    
    self.videoArray = array;
    [self.videoTV reloadData];
    
//    [self.popController.view removeFromSuperview];
//    [self.popController removeFromParentViewController];
    
    //调用showHeaderView协议方法
    [self.showDelegate1 showHeaderView1];
}
- (void)createView{
//    self.videoTV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40*SCALEH)];
    
    self.videoTV.tableHeaderView = view;
    self.videoTV.delegate = self;
    self.videoTV.dataSource = self;
    self.videoTV.separatorStyle = UITableViewCellSeparatorStyleNone;

//    [self.view addSubview:self.videoTV];
}
- (UITableView *)videoTV
{
    if (!_videoTV) {
        _videoTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        [self.view addSubview:self.videoTV];
    }
    return _videoTV;
}
#pragma mark -- UITableview data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.videoArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XJCVideoModel *model = [self.videoArray objectAtIndex:indexPath.row];
    static NSString *cellIdentifier = @"cell";
    XJCTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[XJCTimeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.backgroundColor = [UIColor colorWithRed:0.686 green:1.000 blue:0.885 alpha:1.000];
    cell.videoModel = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJCTimeTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
    CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
    
    EntertainmentDetailsViewController *detailVC = [[EntertainmentDetailsViewController alloc]init];
    detailVC.tempImage = cell.picImg.image;
    detailVC.rect = rect;
    detailVC.delegate = self;
    detailVC.dataArray = self.videoArray;
    detailVC.view.backgroundColor = [UIColor clearColor];
    
    detailVC.type = 1;
    detailVC.contentSet = indexPath.row;
    
    [self addChildViewController:detailVC];
    [self.view addSubview:detailVC.view];
    self.popController = detailVC;
    NSLog(@"%@",NSStringFromCGRect(rect));
    
    //调用hideHeaderView1协议方法
    [self.hideDelegate1 hideHeaderView1];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
